package com.pqtest.api.controller;

import com.pqtest.api.annotation.ResponseFormat;
import com.pqtest.api.dto.CollectionDto;
import com.pqtest.api.dto.SimulibraryDto;
import com.pqtest.api.service.SimulibraryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.oauth2.provider.OAuth2Authentication;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

@RestController
public class SimulibraryController extends BaseController {
    @Autowired
    SimulibraryService simulibraryService;

    //提交试卷
    @PostMapping("/simulibrary")
    @ResponseFormat(type = "withMsg")
    Object submit(@Valid SimulibraryDto simulibraryDto, BindingResult bindingResult, OAuth2Authentication oAuth2Authentication) throws Exception {
        if(bindingResult.hasErrors()){
            throw new Exception(bindingResult.getFieldError().getDefaultMessage());
        }
        String uid = this.getUid(oAuth2Authentication);
        simulibraryDto.setUserId(Integer.parseInt(uid));
        simulibraryService.submit(simulibraryDto);
        return "Successfully done";
    }

    //答题报告
    @PostMapping("/simulibrary/report")
    @ResponseFormat(type = "withSingle")
    Object getreport(@Valid SimulibraryDto simulibraryDto, BindingResult bindingResult, OAuth2Authentication oAuth2Authentication) throws Exception {
        if(bindingResult.hasErrors()){
            throw new Exception(bindingResult.getFieldError().getDefaultMessage());
        }
        return simulibraryService.getReport(simulibraryDto,Integer.parseInt(this.getUid(oAuth2Authentication))).viewTransform();
    }
}
